package com.serene.authority.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serene.authority.admin.entity.vo.MenuVo;
import com.serene.authority.admin.entity.vo.MenuIndexVo;
import com.serene.authority.admin.entity.MyMenu;
import com.serene.authority.admin.mapper.MenuMapper;
import com.serene.authority.admin.mapper.RoleMenuMapper;
import com.serene.authority.admin.service.MenuService;
import com.serene.authority.admin.service.RoleMenuService;
import com.serene.authority.common.utils.Result;
import com.serene.authority.common.utils.TreeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static cn.hutool.core.date.DateTime.now;

/**
 * @author serene
 * @createTime 2020/7/10
 */

@Service
@Slf4j
public class MenuServiceImpl extends ServiceImpl<MenuMapper, MyMenu> implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private RoleMenuService roleMenuService;

    @Override
    public List<MyMenu> getMenuAll(String queryName, Integer queryType) {

        return menuMapper.getFuzzyMenu(queryName, queryType);
    }

    @Override
    public MyMenu getMenuById(Integer id) {
        QueryWrapper<MyMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("menu_id", id);
        return menuMapper.selectOne(wrapper);
    }

    @Override
    public List<MenuVo> buildMenuAll() {
        return menuMapper.buildAll();
    }

    @Override
    public Result updateMenu(MyMenu menu) {
        menu.setIcon("layui-icon " + menu.getIcon());
        return (menuMapper.update(menu) > 0) ? Result.ok().message("修改成功") : Result.error().message("修改失败");

    }

    @Override
    public Result saveMenu(MyMenu menu) {
        menu.setIcon("layui-icon " + menu.getIcon());
        menu.setCreateTime(now());
        menu.setUpdateTime(now());
        return (menuMapper.insert(menu) > 0) ? Result.ok().message("添加成功") : Result.error().message("添加失败");

    }


    /**
     * 如果这里删除了菜单树的父节点，把它的子节点一并删除
     *
     * @param id
     * @return
     */
    @Override
    public Result delete(Integer id) {
        System.out.println("id-->" + id);
        Integer count = roleMenuService.queryRoleMenuCountByMenuId(id);
        if (count == 0) {
            //通过菜单id删除菜单
            deleteMenuById(id);
            List<Integer> list = menuMapper.selectByParentId(id);
            if (list.size() > 0) {
                for (Integer parentId : list) {
                    delMenuById(parentId);
                }
                //通过父节点删除子菜单
                delMenuById(id);
            }
            return Result.ok().message("删除成功");
        } else {
            return Result.error().message("已经有角色分配该菜单，无法删除");
        }

    }


    /**
     * 通过父节点删除子菜单
     *
     * @param parentId 父级菜单id
     * @return
     */
    @Override
    public Integer delMenuById(Integer parentId) {
        QueryWrapper<MyMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", parentId);
        return menuMapper.delete(wrapper);
    }

    /**
     * @param menuId 菜单id
     * @return
     */
    @Override
    public Integer deleteMenuById(Integer menuId) {
        QueryWrapper<MyMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("menu_id", menuId);
        return menuMapper.delete(wrapper);
    }

    @Override
    public List<MenuVo> buildMenuAllByRoleId(Integer roleId) {
        List<MenuVo> listByRoleId = menuMapper.listByRoleId(roleId);
        List<MenuVo> permissionDtos = menuMapper.buildAll();
        List<MenuVo> tree = TreeUtil.menutree(listByRoleId, permissionDtos);
        return tree;
    }

    @Override
    public List<MenuIndexVo> getMenu(Integer userId) {
        List<MenuIndexVo> list = menuMapper.listByUserId(userId);
        List<MenuIndexVo> result = TreeUtil.parseMenuTree(list);
        return result;
    }
}
